package com.mirror.llk.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.Log;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.OnActionCompleted;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;
import com.badlogic.gdx.scenes.scene2d.actions.FadeOut;
import com.badlogic.gdx.scenes.scene2d.actions.MoveTo;
import com.badlogic.gdx.scenes.scene2d.actions.Parallel;
import com.badlogic.gdx.scenes.scene2d.actions.Sequence;
import com.badlogic.gdx.scenes.scene2d.actors.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.mirror.llk.GameMainActivity;
import com.mirror.llk.bean.domain.GameBean;
import com.mirror.llk.task.GameTask;

/**
 * 连连看游戏主界面.
 * <p>
 * Copyright: Copyright (c) 2013-7-17 下午1:56:48
 * <p>
 * Company: DIY工作室
 * <p>
 * Author: 李晖
 * <p>
 * Version: 1.0
 * <p>
 */
public class GameView implements ApplicationListener {

	/** 背景，选中，消除音乐 */
	private Music backMusic, selectMusic, correctMusic, loseMusic;

	/** 舞台 */
	private Stage stage;

	/** 游戏主页上下文，由构造函数传入 */
	private GameMainActivity activity;

	/** 游戏实例化对象 */
	private GameBean gameBean;

	/** 是否正在游戏，暂停或尚未开始或关卡之间时间为false */
	private boolean gameStatus = false;

	/** 字体样式 */
	private BitmapFont bitmapFont;

	/** 文字显示所需的Label */
	private Label gameOverLabel, gameLevelLabel, gameDescLabel;

	/** 动画时长 */
	private float duration = 0.5f;

	private int[] firstPosition = null;

	private Button selectorBtn;

	private Button selectorBtn2; // 提示可消除图片时的第二个图

	/** 成功消除的次数 */
	private int successNumber = 0;

	/** 手机屏宽显示图片数目 */
	private static int X_NUMBER = 8;

	/** 手机屏高显示图片数目 */
	private static int Y_NUMBER = 10;

	/** 游戏区域显示图片数目 */
	private static int XY_NUMBER = X_NUMBER * Y_NUMBER;

	/** 最大消除次数，如果达到说明过关 */
	private static int MAX_SUCCESS_NUMBER = XY_NUMBER / 2;

	private ButtonStyle[] picButtonStyle = new ButtonStyle[18];

	private Button[][] picBtnArray = new Button[Y_NUMBER][X_NUMBER];

	private int[][][] picBtnIntArray = new int[Y_NUMBER][X_NUMBER][2];

	private static int PIC_BTN_INT_ARRAY_EMPTY = -1;

	/** 图片的二维数组，包含图片信息 */
	private int[][] picIntArray = new int[Y_NUMBER][X_NUMBER];

	/** 折线点集合 */
	private List<Point> path = new ArrayList<Point>();

	/** 消除图片时所画的线 */
	private Mesh[] lineMesh = new Mesh[3];

	private float iconSize;

	/** 帮助数 */
	private int help = 3;

	/** 刷新数 */
	private int refresh = 3;

	/**
	 * @param context
	 */
	public GameView(GameMainActivity activity, GameBean gameBean) {
		this.activity = activity;
		this.gameBean = gameBean;
		calIconSize();
	}

	/**
	 * 计算图标的长宽
	 */
	private void calIconSize() {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		iconSize = dm.widthPixels / (X_NUMBER);
	}

	/**
	 * 游戏主页初始化
	 */
	@Override
	public void create() {
		/** 第一步、初始化音乐 */
		backMusic = Gdx.audio.newMusic(Gdx.files.internal("bg.ogg"));
		backMusic.setLooping(true);
		backMusic.setVolume(10f);
		selectMusic = Gdx.audio.newMusic(Gdx.files.internal("choose.ogg"));
		selectMusic.setLooping(false);
		selectMusic.setVolume(15f);
		correctMusic = Gdx.audio.newMusic(Gdx.files.internal("success.ogg"));
		correctMusic.setLooping(false);
		correctMusic.setVolume(15f);

		/** 第二步、初始化游戏图片 */
		bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"), Gdx.files.internal("cf.png"), false);
		// 加载图片
		for (int i = 0; i < picButtonStyle.length; i++) {
			Log.e("lihui", "theme/1/" + (i + 1) + ".png");
			Texture picTexture = new Texture(Gdx.files.internal("theme/1/" + (i + 1) + ".png"));
			NinePatch ninePath = new NinePatch(picTexture, 7, 7, 9, 9);
			picButtonStyle[i] = new ButtonStyle(ninePath, ninePath, ninePath, 0f, 0f, 0f, 0f, bitmapFont, new Color(1, 1, 0, 0.5f));
		}
		Texture picTexture = new Texture(Gdx.files.internal("selector.png"));
		NinePatch ninePath = new NinePatch(picTexture, 7, 7, 9, 9);
		ButtonStyle selectButtonStyle = new ButtonStyle(ninePath, ninePath, ninePath, 0f, 0f, 0f, 0f, bitmapFont, new Color(1, 1, 0, 0.5f));
		selectorBtn = new Button("", selectButtonStyle, "selectorBtn");
		selectorBtn2 = new Button("", selectButtonStyle, "selectorBtn");
		int size = Gdx.graphics.getWidth() / X_NUMBER + 20;
		selectorBtn.width = size;
		selectorBtn.height = size;
		selectorBtn.x = Gdx.graphics.getWidth();
		selectorBtn.y = Gdx.graphics.getHeight();

		selectorBtn2.width = size;
		selectorBtn2.height = size;
		selectorBtn2.x = Gdx.graphics.getWidth();
		selectorBtn2.y = Gdx.graphics.getHeight();

		/** 第三步、初始化舞台 */
		stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
		stage.addActor(selectorBtn);
		stage.addActor(selectorBtn2);
		Gdx.input.setInputProcessor(stage);
		for (int i = 0; i < lineMesh.length; i++) {
			lineMesh[i] = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, "line_position"));
			lineMesh[i].setIndices(new short[] { 0, 1 });// 设置顶点索引
		}

		/** 第四步、异步加载游戏数据，稍后调用startGame方法 */
		new GameTask(this, gameBean).execute();
	}

	/**
	 * 游戏开始
	 */
	public void startGame() {
		/** 第一步、初始化时间控件 */
		activity.setClearPbRemainingTime(true);
		activity.setTotalRemainingTime(GAME_LEVEL_TOTAL_TIME.get(gameBean.getLevel()));

		/** 第一步、隐藏关卡信息 */
		boolean flag = false;
		// 关卡开始前提示
		if (gameLevelLabel == null) {
			gameLevelLabel = new Label("gameLevel", bitmapFont, GAME_LEVEL_TEXT.get(gameBean.getLevel()));
			gameDescLabel = new Label("gameDesc", bitmapFont, GAME_LEVEL_DESC.get(gameBean.getLevel()));
			flag = true;
		} else {
			gameLevelLabel.setText(GAME_LEVEL_TEXT.get(gameBean.getLevel()));
			gameDescLabel.setText(GAME_LEVEL_DESC.get(gameBean.getLevel()));
		}
		gameLevelLabel.x = (Gdx.graphics.getWidth() - gameLevelLabel.width) / 2;
		gameLevelLabel.y = Gdx.graphics.getHeight() + 110;

		gameDescLabel.x = (Gdx.graphics.getWidth() - gameDescLabel.width) / 2;
		gameDescLabel.y = Gdx.graphics.getHeight() + 10;

		Action levelAction = Sequence.$(MoveTo.$((Gdx.graphics.getWidth() - gameLevelLabel.width) / 2,
		                                         (Gdx.graphics.getHeight() - gameLevelLabel.height) / 2 + 50, 2 * duration), MoveTo.$((Gdx.graphics
		        .getWidth() - gameLevelLabel.width) / 2, (Gdx.graphics.getHeight() - gameLevelLabel.height) / 2 + 50, duration), MoveTo
		        .$((Gdx.graphics.getWidth() - gameLevelLabel.width) / 2, 0 - 10 - gameLevelLabel.height, duration));
		gameLevelLabel.action(Sequence.$(Delay.$(levelAction, 2 * duration)));

		Action descAction = Sequence.$(MoveTo.$((Gdx.graphics.getWidth() - gameDescLabel.width) / 2,
		                                        (Gdx.graphics.getHeight() - gameDescLabel.height) / 2 - 50, 2 * duration), MoveTo.$((Gdx.graphics
		        .getWidth() - gameDescLabel.width) / 2, (Gdx.graphics.getHeight() - gameDescLabel.height) / 2 - 50, duration), MoveTo.$((Gdx.graphics
		        .getWidth() - gameDescLabel.width) / 2, 0 - 110 - gameDescLabel.height, duration));
		gameDescLabel.action(Sequence.$(Delay.$(descAction, 2 * duration)));
		if (flag) {
			stage.addActor(gameLevelLabel);
			stage.addActor(gameDescLabel);
		}

		/** 第二步、初始化游戏界面 */
		initGameInt(gameBean.getLevel());
		initGameBtn(true);

		/** 第三步、开启音乐 */
		backMusic.play();
	}

	/**
	 * 过关
	 */
	public void passLevel() {
		setGameStatus(false);
		clearSuccessNumber();
		if (gameBean.getLevel() == GAME_LEVEL_DESC.size()) {
			passGame();
		} else {
			gameBean.setLevel(gameBean.getLevel() + 1);
			// 增加提醒、洗牌次数
			refresh++;
			help += 2;
			activity.notifyChangeNum(false);
			activity.notifyHelpNum();
			startGame();
		}
	}

	/**
	 * 游戏结束
	 */
	public void overGame() {
		// 暂停背景音乐
		backMusic.pause();
		setGameStatus(false);
		for (int i = 0; i < Y_NUMBER; i++) {
			for (int j = 0; j < X_NUMBER; j++) {
				if (picBtnArray[i][j] != null) {
					stage.removeActor(picBtnArray[i][j]);
				}
			}
		}
		stage.removeActor(selectorBtn);
		stage.removeActor(selectorBtn2);
		loseMusic = Gdx.audio.newMusic(Gdx.files.internal("lose.ogg"));
		loseMusic.setLooping(false);
		loseMusic.setVolume(15f);
		loseMusic.play();

		overGameTextShow("Game Over!");
		rating();
	}

	/**
	 * 游戏通关
	 */
	public void passGame() {
		overGameTextShow("You Win!");
		rating();
	}

	/**
	 * 游戏结束文本提示
	 * 
	 * @param text
	 */
	public void overGameTextShow(String text) {
		gameOverLabel = new Label("gameOver", bitmapFont, text);
		gameOverLabel.x = (Gdx.graphics.getWidth() - gameOverLabel.width) / 2;
		gameOverLabel.y = Gdx.graphics.getHeight();
		// 移动
		Action moveAction = Sequence.$(MoveTo.$((Gdx.graphics.getWidth() - gameOverLabel.width) / 2,
		                                        (Gdx.graphics.getHeight() - gameOverLabel.height) / 2, duration));
		// 逐渐显示
		Action fadeAction = Sequence.$(Sequence.$(FadeOut.$(0), FadeIn.$(duration)));
		// 添加事件组合
		gameOverLabel.action(Parallel.$(moveAction, fadeAction));

		stage.addActor(gameOverLabel);
	}

	/**
	 * 提示是否上传比分
	 */
	public void rating() {
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	@Override
	public void pause() {
		backMusic.pause();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
		if (path != null) {
			if (path.size() != 0) {
				Log.e("render", "path.size()=" + path.size());
			}
			if (path.size() == 2) {
				float x1 = path.get(0).x + 0.5f;
				float y1 = Y_NUMBER - path.get(0).y - 0.5f;
				float x2 = path.get(1).x + 0.5f;
				float y2 = Y_NUMBER - path.get(1).y - 0.5f;

				lineMesh[0].setVertices(new float[] { x1 * iconSize, y1 * iconSize, 0, x2 * iconSize, y2 * iconSize, 0 });// 设置将要画线的两个顶点
				lineMesh[0].render(GL11.GL_LINES, 0, 2);
				path.clear();
			} else if (path.size() == 3) {
				float x1 = path.get(0).x + 0.5f;
				float y1 = Y_NUMBER - path.get(0).y - 0.5f;
				float x2 = path.get(1).x + 0.5f;
				float y2 = Y_NUMBER - path.get(1).y - 0.5f;
				float x3 = path.get(2).x + 0.5f;
				float y3 = Y_NUMBER - path.get(2).y - 0.5f;

				lineMesh[0].setVertices(new float[] { x1 * iconSize, y1 * iconSize, 0, x2 * iconSize, y2 * iconSize, 0 });
				lineMesh[1].setVertices(new float[] { x2 * iconSize, y2 * iconSize, 0, x3 * iconSize, y3 * iconSize, 0 });
				lineMesh[0].render(GL11.GL_LINES, 0, 2);
				lineMesh[1].render(GL11.GL_LINES, 0, 2);
				path.clear();
			} else if (path.size() == 4) {
				float x1 = path.get(0).x + 0.5f;
				float y1 = Y_NUMBER - path.get(0).y - 0.5f;
				float x2 = path.get(1).x + 0.5f;
				float y2 = Y_NUMBER - path.get(1).y - 0.5f;
				float x3 = path.get(2).x + 0.5f;
				float y3 = Y_NUMBER - path.get(2).y - 0.5f;
				float x4 = path.get(3).x + 0.5f;
				float y4 = Y_NUMBER - path.get(3).y - 0.5f;

				lineMesh[0].setVertices(new float[] { x1 * iconSize, y1 * iconSize, 0, x2 * iconSize, y2 * iconSize, 0 });
				lineMesh[1].setVertices(new float[] { x2 * iconSize, y2 * iconSize, 0, x3 * iconSize, y3 * iconSize, 0 });
				lineMesh[2].setVertices(new float[] { x3 * iconSize, y3 * iconSize, 0, x4 * iconSize, y4 * iconSize, 0 });
				lineMesh[0].render(GL11.GL_LINES, 0, 2);
				lineMesh[1].render(GL11.GL_LINES, 0, 2);
				lineMesh[2].render(GL11.GL_LINES, 0, 2);
				path.clear();
			}
		}
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {
		if (isGameStatus()) {
			backMusic.play();
		}
	}

	/**
	 * 初始化游戏，成双生成图片数组
	 * 
	 * @param level
	 *            关卡
	 * @return
	 */
	public void initGameInt(int level) {
		// 初始化游戏界面
		int[] arr = new int[XY_NUMBER];
		for (int i = 0; i < XY_NUMBER; i++) {
			arr[i] = i;
		}
		for (int i = 0; i < MAX_SUCCESS_NUMBER; i++) {
			// 循环图片，使每个图片出现的概率相同
			int number = i % GAME_LEVEL_PIC_NUMBERS.get(level) + 1;
			int position1 = MathUtils.random(0, XY_NUMBER - 1 - i * 2);
			picIntArray[arr[position1] / X_NUMBER][arr[position1] % X_NUMBER] = number;
			arr[position1] = arr[XY_NUMBER - 1 - i * 2];
			int position2 = MathUtils.random(0, XY_NUMBER - 2 - i * 2);
			picIntArray[arr[position2] / X_NUMBER][arr[position2] % X_NUMBER] = number;
			arr[position2] = arr[XY_NUMBER - 2 - i * 2];
		}
		/*
		 * if(die()) //如果死锁，重新初始化 { change(); }
		 */
	}

	/**
	 * 初始化图片
	 * 
	 * @param isAnimal
	 *            是否有动画
	 */
	public void initGameBtn(boolean isAnimal) {
		int size = Gdx.graphics.getWidth() / X_NUMBER;
		for (int i = 0; i < Y_NUMBER; i++) {
			for (int j = 0; j < X_NUMBER; j++) {
				picBtnIntArray[i][j][0] = i;
				picBtnIntArray[i][j][1] = j;
				if (picIntArray[i][j] == 0) {
					continue;
				}
				picBtnArray[i][j] = new Button("", picButtonStyle[picIntArray[i][j] - 1], i + "_" + j);
				picBtnArray[i][j].width = size;
				picBtnArray[i][j].height = size;
				if (isAnimal) {
					picBtnArray[i][j].x = j * size;
					picBtnArray[i][j].y = 2 * Gdx.graphics.getHeight() - (i + 1) * size;
				} else {
					picBtnArray[i][j].x = j * size;
					picBtnArray[i][j].y = Gdx.graphics.getHeight() - (i + 1) * size;
				}
				picBtnArray[i][j].setClickListener(new ClickListener() {

					@Override
					public void click(Actor actor) {

						String[] position = actor.name.split("_");
						int[] intPosition = { Integer.valueOf(position[0]), Integer.valueOf(position[1]) };
						if (firstPosition == null) {
							firstPosition = intPosition;
							selectorBtn.x = actor.x - 10;
							selectorBtn.y = actor.y - 10;
							selectorBtn2.x = Gdx.graphics.getWidth(); // 取消第二个图片的选择
							selectorBtn2.y = Gdx.graphics.getHeight();
							selectMusic.stop();
							selectMusic.play();
						} else {
							boolean flag = checkPosition(intPosition, firstPosition);
							if (flag) {
								correctMusic.stop();
								correctMusic.play();
								stage.removeActor(picBtnArray[firstPosition[0]][firstPosition[1]]);
								stage.removeActor(picBtnArray[intPosition[0]][intPosition[1]]);
								selectorBtn.x = Gdx.graphics.getWidth();
								selectorBtn.y = Gdx.graphics.getHeight();
								successNumber++;
								activity.setAddTime(activity.getAddTime() + 1);
								boolean isPass = false;
								if (successNumber == MAX_SUCCESS_NUMBER) {
									passLevel();
									isPass = true;
								} else {
									doSomthingAfterSuccess(intPosition, firstPosition);
								}
								firstPosition = null;

								if (!isPass) {
									if (die(new ArrayList<Point>())) {// 每次消除一个，都判断下是否死锁
										if (!(activity.notifyChangeNum(true))) {
											overGame();
										}
									}
								}
							} else {
								firstPosition = intPosition;
								selectorBtn.x = actor.x - 10;
								selectorBtn.y = actor.y - 10;
								selectMusic.stop();
								selectMusic.play();
							}
						}
					}
				});
				if (isAnimal) {
					Action actionBtn = Sequence.$(MoveTo.$(j * size, Gdx.graphics.getHeight() - (i + 1) * size,
					                                       2.5f - i * 0.2f + MathUtils.random(0, 0.2f)));
					if (i == 0 && j == 0) {// 左上角图片action完成，认为游戏加载结束
						actionBtn.setCompletionListener(new OnActionCompleted() {

							public void completed(Action action) {
								// 开始计时
								setGameStatus(true);
							}
						});
					}
					picBtnArray[i][j].action(Sequence.$(Delay.$(actionBtn, 6 * duration)));
				}
				stage.addActor(picBtnArray[i][j]);
			}
		}
	}

	/**
	 * 校验两个位置是否能够消除
	 * 
	 * @param oldPosition
	 * @param newPosition
	 * @return true-能消;false-不能消
	 */
	public boolean checkPosition(int[] oldPosition, int[] newPosition) {
		if (oldPosition == null || newPosition == null) {
			return false;
		}
		if (oldPosition[0] == newPosition[0] && oldPosition[1] == newPosition[1]) {
			return false;
		}
		int[] oldP = picBtnIntArray[oldPosition[0]][oldPosition[1]];
		int[] newP = picBtnIntArray[newPosition[0]][newPosition[1]];
		if (picIntArray[oldP[0]][oldP[1]] == picIntArray[newP[0]][newP[1]]) {

			if (link(oldP, newP, path)) {
				picIntArray[oldP[0]][oldP[1]] = picIntArray[newP[0]][newP[1]] = 0;
				return true;
			} else
				return false;
		} else {
			return false;
		}
	}

	/** 和p1处于同一直线上的点 */
	List<Point> p1E = new ArrayList<Point>();

	/** 和p2处于同一直线上的点 */
	List<Point> p2E = new ArrayList<Point>();

	/**
	 * 判断2点能否消除
	 * 
	 * @param p1
	 * @param p2
	 * @return
	 */
	private boolean link(int[] oldP, int[] newP, List<Point> path) {
		Point p1 = new Point(oldP[1], oldP[0]);
		Point p2 = new Point(newP[1], newP[0]);
		path.clear();
		if (linkD(p1, p2)) {
			path.add(p1);
			path.add(p2);
			return true;
		}

		Point p = new Point(p1.x, p2.y);
		if (picIntArray[p.y][p.x] == 0) {
			if (linkD(p1, p) && linkD(p, p2)) {
				path.add(p1);
				path.add(p);
				path.add(p2);
				return true;
			}
		}
		p = new Point(p2.x, p1.y);
		if (picIntArray[p.y][p.x] == 0) {
			if (linkD(p1, p) && linkD(p, p2)) {
				path.add(p1);
				path.add(p);
				path.add(p2);
				return true;
			}
		}
		expandX(p1, p1E);
		expandX(p2, p2E);

		for (Point pt1 : p1E) {
			for (Point pt2 : p2E) {
				if (pt1.x == pt2.x) {
					if (linkD(pt1, pt2)) {
						path.add(p1);
						path.add(pt1);
						path.add(pt2);
						path.add(p2);
						return true;
					}
				}
			}
		}

		expandY(p1, p1E);
		expandY(p2, p2E);
		for (Point pt1 : p1E) {
			for (Point pt2 : p2E) {
				if (pt1.y == pt2.y) {
					if (linkD(pt1, pt2)) {
						path.add(p1);
						path.add(pt1);
						path.add(pt2);
						path.add(p2);
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 判断2点在同一直线上是否连通
	 * 
	 * @param p1
	 * @param p2
	 * @return
	 */
	private boolean linkD(Point p1, Point p2) {
		if (p1.x == p2.x) {
			int y1 = Math.min(p1.y, p2.y);
			int y2 = Math.max(p1.y, p2.y);
			boolean flag = true;
			for (int y = y1 + 1; y < y2; y++) {
				if (picIntArray[y][p1.x] != 0) {
					flag = false;
					break;
				}
			}
			if (flag) {
				return true;
			}
		}
		if (p1.y == p2.y) {
			int x1 = Math.min(p1.x, p2.x);
			int x2 = Math.max(p1.x, p2.x);
			boolean flag = true;
			for (int x = x1 + 1; x < x2; x++) {
				if (picIntArray[p1.y][x] != 0) {
					flag = false;
					break;
				}
			}
			if (flag) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取与p处于同一水平线上的点，不包括p
	 * 
	 * @param p
	 * @param l
	 */
	private void expandX(Point p, List<Point> l) {
		l.clear();
		for (int x = p.x + 1; x < X_NUMBER; x++) {
			if (picIntArray[p.y][x] != 0) {
				break;
			}
			l.add(new Point(x, p.y));
		}
		for (int x = p.x - 1; x >= 0; x--) {
			if (picIntArray[p.y][x] != 0) {
				break;
			}
			l.add(new Point(x, p.y));
		}
	}

	/**
	 * 获取与p处于同一竖直线上的点，不包括p
	 * 
	 * @param p
	 * @param l
	 */
	private void expandY(Point p, List<Point> l) {
		l.clear();
		for (int y = p.y + 1; y < Y_NUMBER; y++) {
			if (picIntArray[y][p.x] != 0) {
				break;
			}
			l.add(new Point(p.x, y));
		}
		for (int y = p.y - 1; y >= 0; y--) {
			if (picIntArray[y][p.x] != 0) {
				break;
			}
			l.add(new Point(p.x, y));
		}
	}

	/**
	 * 消除成功后，对图片进行相应的移动
	 * 
	 * @param oldPosition
	 * @param newPosition
	 */
	public void doSomthingAfterSuccess(int[] oldPosition, int[] newPosition) {
		switch (gameBean.getLevel()) {
			case 1:
				break;
			case 2:
				break;
			case 3:// 上下分离
				upDownSeparation(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 4:// 左右分离
				leftRightSeparation(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 5:// 向左看齐
				leftConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 6:// 地心引力
				downConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 7:// 飘向天空
				upConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 8:// 向右看齐
				rightConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 9:// 左右集中
				leftRightConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 10:// 上下集中
				upDownConcentrate(oldPosition[0], oldPosition[1], newPosition[0], newPosition[1]);
				break;
			case 11:// 四周分离

				break;
			case 12:// 中央集中
				// centerConcentrate(oldPosition[0], oldPosition[1],
				// newPosition[0], newPosition[1]);
				break;
			default:
				break;
		}

		for (int i = 0; i < picIntArray.length; i++) {
			String str = "";
			for (int j = 0; j < picIntArray[i].length; j++) {
				str += picIntArray[i][j] + "\t";
			}
			Log.e("doSomthingAfterSuccess", str);
		}

		for (int i = 0; i < picBtnIntArray.length; i++) {
			String str = "";
			for (int j = 0; j < picBtnIntArray[i].length; j++) {
				str += picBtnIntArray[i][j][0] + "_" + picBtnIntArray[i][j][1] + "\t";
			}
			Log.e("doSomthingAfterSuccess", str);
		}
	}

	/**
	 * 上下分离
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void upDownSeparation(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int harfLength = Y_NUMBER / 2 - 1;
		if (i1 < harfLength && i2 < harfLength) {
			if (i1 > i2) {
				upDownSeparation(i1, j1, map);
				upDownSeparation(i2, j2, map);
			} else {
				upDownSeparation(i2, j2, map);
				upDownSeparation(i1, j1, map);
			}
		} else if (i1 > harfLength + 1 && i2 > harfLength + 1) {
			if (i1 > i2) {
				upDownSeparation(i2, j2, map);
				upDownSeparation(i1, j1, map);
			} else {
				upDownSeparation(i1, j1, map);
				upDownSeparation(i2, j2, map);
			}
		} else {
			upDownSeparation(i1, j1, map);
			upDownSeparation(i2, j2, map);
		}
	}

	/**
	 * 上下分离
	 * 
	 * @param i
	 * @param j
	 */
	public void upDownSeparation(int i, int j, Map<String, Integer> map) {
		int harfLength = Y_NUMBER / 2 - 1;
		if (i == harfLength || i == harfLength + 1) {
			picIntArray[picBtnIntArray[i][j][0]][picBtnIntArray[i][j][1]] = 0;
			picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
			picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
			return;
		} else if (i < harfLength) {
			up(i, j, map, harfLength);
		} else if (i > harfLength + 1) {
			down(i, j, map, harfLength + 1);
		}
	}

	/**
	 * 左右分离
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void leftRightSeparation(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int harfLength = X_NUMBER / 2 - 1;
		if (j1 < harfLength && j2 < harfLength) {
			if (j1 > j2) {
				leftRightSeparation(i1, j1, map);
				leftRightSeparation(i2, j2, map);
			} else {
				leftRightSeparation(i2, j2, map);
				leftRightSeparation(i1, j1, map);
			}
		} else if (j1 > harfLength + 1 && j2 > harfLength + 1) {
			if (j1 > j2) {
				leftRightSeparation(i2, j2, map);
				leftRightSeparation(i1, j1, map);
			} else {
				leftRightSeparation(i1, j1, map);
				leftRightSeparation(i2, j2, map);
			}
		} else {
			leftRightSeparation(i1, j1, map);
			leftRightSeparation(i2, j2, map);
		}
	}

	/**
	 * 左右分离
	 * 
	 * @param i
	 * @param j
	 */
	public void leftRightSeparation(int i, int j, Map<String, Integer> map) {
		int harfLength = X_NUMBER / 2 - 1;
		if (j == harfLength || j == harfLength + 1) {
			picIntArray[picBtnIntArray[i][j][0]][picBtnIntArray[i][j][1]] = 0;
			picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
			picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
			return;
		} else if (j < harfLength) {
			left(i, j, map, harfLength);
		} else if (j > harfLength + 1) {
			right(i, j, map, harfLength + 1);
		}
	}

	/**
	 * 向左看齐
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void leftConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (j1 > j2) {
			leftConcentrate(i1, j1, map);
			leftConcentrate(i2, j2, map);
		} else {
			leftConcentrate(i2, j2, map);
			leftConcentrate(i1, j1, map);
		}
	}

	/**
	 * 向左看齐
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void leftConcentrate(int i, int j, Map<String, Integer> map) {
		left(i, j, map, X_NUMBER - 1);
	}

	/**
	 * 飘向天空
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void upConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (i1 > i2) {
			upConcentrate(i1, j1, map);
			upConcentrate(i2, j2, map);
		} else {
			upConcentrate(i2, j2, map);
			upConcentrate(i1, j1, map);
		}
	}

	/**
	 * 飘向天空
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void upConcentrate(int i, int j, Map<String, Integer> map) {
		up(i, j, map, Y_NUMBER - 1);
	}

	/**
	 * 地心引力
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void downConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (j1 > j2) {
			downConcentrate(i2, j2, map);
			downConcentrate(i1, j1, map);
		} else {
			downConcentrate(i1, j1, map);
			downConcentrate(i2, j2, map);
		}
	}

	/**
	 * 地心引力
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void downConcentrate(int i, int j, Map<String, Integer> map) {
		down(i, j, map, 0);
	}

	/**
	 * 向右看齐
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void rightConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (i1 > i2) {
			rightConcentrate(i2, j2, map);
			rightConcentrate(i1, j1, map);
		} else {
			rightConcentrate(i1, j1, map);
			rightConcentrate(i2, j2, map);
		}
	}

	/**
	 * 向右看齐
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void rightConcentrate(int i, int j, Map<String, Integer> map) {
		right(i, j, map, 0);
	}

	/**
	 * 左右集中
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void leftRightConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int harfLength = X_NUMBER / 2 - 1;
		if (j1 < harfLength && j2 < harfLength) {
			if (j2 > j1) {
				leftRightConcentrate(i1, j1, map);
				leftRightConcentrate(i2, j2, map);
			} else {
				leftRightConcentrate(i2, j2, map);
				leftRightConcentrate(i1, j1, map);
			}
		} else if (j1 > harfLength + 1 && j2 > harfLength + 1) {
			if (j2 > j1) {
				leftRightConcentrate(i2, j2, map);
				leftRightConcentrate(i1, j1, map);
			} else {
				leftRightConcentrate(i1, j1, map);
				leftRightConcentrate(i2, j2, map);
			}
		} else {
			leftRightConcentrate(i1, j1, map);
			leftRightConcentrate(i2, j2, map);
		}
	}

	/**
	 * 左右集中
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void leftRightConcentrate(int i, int j, Map<String, Integer> map) {
		int harfLength = X_NUMBER / 2 - 1;
		if (j == 0 || j == X_NUMBER - 1) {
			picIntArray[picBtnIntArray[i][j][0]][picBtnIntArray[i][j][1]] = 0;
			picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
			picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
			return;
		} else if (j <= harfLength) {
			right(i, j, map, 0);
		} else if (j > harfLength) {
			left(i, j, map, X_NUMBER - 1);
		}
	}

	/**
	 * 上下集中
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void upDownConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int harfLength = Y_NUMBER / 2 - 1;
		if (i1 < harfLength && i2 < harfLength) {
			if (i2 > i1) {
				upDownConcentrate(i1, j1, map);
				upDownConcentrate(i2, j2, map);
			} else {
				upDownConcentrate(i2, j2, map);
				upDownConcentrate(i1, j1, map);
			}
		} else if (i1 > harfLength + 1 && i2 > harfLength + 1) {
			if (i2 > i1) {
				upDownConcentrate(i2, j2, map);
				upDownConcentrate(i1, j1, map);
			} else {
				upDownConcentrate(i1, j1, map);
				upDownConcentrate(i2, j2, map);
			}
		} else {
			upDownConcentrate(i1, j1, map);
			upDownConcentrate(i2, j2, map);
		}
	}

	/**
	 * 上下集中
	 * 
	 * @param i
	 * @param j
	 */
	public void upDownConcentrate(int i, int j, Map<String, Integer> map) {
		int harfLength = Y_NUMBER / 2 - 1;
		if (i == 0 || i == Y_NUMBER - 1) {
			picIntArray[picBtnIntArray[i][j][0]][picBtnIntArray[i][j][1]] = 0;
			picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
			picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
			return;
		} else if (i <= harfLength) {
			down(i, j, map, 0);
		} else if (i > harfLength) {
			up(i, j, map, Y_NUMBER - 1);
		}
	}

	/**
	 * 中央集中
	 * 
	 * @param i1
	 * @param j1
	 * @param i2
	 * @param j2
	 */
	public void centerConcentrate(int i1, int j1, int i2, int j2) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int harfLength = Y_NUMBER / 2 - 1;
		if (i1 < harfLength && i2 < harfLength) {
			if (i2 > i1) {
				centerConcentrate(i1, j1, map);
				centerConcentrate(i2, j2, map);
			} else {
				centerConcentrate(i2, j2, map);
				centerConcentrate(i1, j1, map);
			}
		} else if (i1 > harfLength + 1 && i2 > harfLength + 1) {
			if (i2 > i1) {
				centerConcentrate(i2, j2, map);
				centerConcentrate(i1, j1, map);
			} else {
				centerConcentrate(i1, j1, map);
				centerConcentrate(i2, j2, map);
			}
		} else {
			centerConcentrate(i1, j1, map);
			centerConcentrate(i2, j2, map);
		}
	}

	/**
	 * 中央集中
	 * 
	 * @param i
	 * @param j
	 * @param map
	 */
	public void centerConcentrate(int i, int j, Map<String, Integer> map) {
		int harfLength = Y_NUMBER / 2 - 1;
		if (i == 0 || i == Y_NUMBER - 1) {
			picIntArray[picBtnIntArray[i][j][0]][picBtnIntArray[i][j][1]] = 0;
			picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
			picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
			return;
		} else if (i <= harfLength) {
			down(i, j, map, 0);
		} else if (i > harfLength) {
			up(i, j, map, Y_NUMBER - 1);
		}
	}

	/**
	 * 向上移动
	 * 
	 * @param i
	 * @param j
	 * @param map
	 * @param length
	 */
	public void up(int i, int j, Map<String, Integer> map, int length) {
		// 向上移动一格
		int size = Gdx.graphics.getWidth() / X_NUMBER;
		int tempI = picBtnIntArray[i][j][0], tempJ = picBtnIntArray[i][j][1];

		// 更新picIntArray
		int picX = picBtnIntArray[i][j][0];
		int picY = picBtnIntArray[i][j][1];
		for (int x = picX + 1; x <= length; x++) {
			if (picIntArray[x][picY] != 0) {
				picIntArray[x - 1][picY] = picIntArray[x][picY];
				picIntArray[x][picY] = 0;
			}
		}

		picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
		picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;

		for (int x = i + 1; x <= length; x++) {
			if (picBtnIntArray[x][j][0] != PIC_BTN_INT_ARRAY_EMPTY && picBtnIntArray[x][j][1] != PIC_BTN_INT_ARRAY_EMPTY) {
				int tempII = 0, tempJJ = 0;
				tempII = picBtnIntArray[x][j][0];
				tempJJ = picBtnIntArray[x][j][1];
				picBtnIntArray[x][j][0] = tempI;
				picBtnIntArray[x][j][1] = tempJ;
				tempI = tempII;
				tempJ = tempJJ;
			}

			Integer dy = map.get(x + "_" + j);
			if (dy == null) {
				dy = 0;
			}
			map.put(x + "_" + j, dy + 1);
			Action moveAction = Sequence.$(MoveTo.$(picBtnArray[x][j].x, picBtnArray[x][j].y + (dy + 1) * size, duration / 5));
			picBtnArray[x][j].action(moveAction);
		}
	}

	/**
	 * 向下移动
	 * 
	 * @param i
	 * @param j
	 * @param map
	 * @param length
	 */
	public void down(int i, int j, Map<String, Integer> map, int length) {
		// 向下移动一格
		int size = Gdx.graphics.getWidth() / X_NUMBER;
		int tempI = picBtnIntArray[i][j][0], tempJ = picBtnIntArray[i][j][1];

		int picX = picBtnIntArray[i][j][0];
		int picY = picBtnIntArray[i][j][1];
		picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
		picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
		for (int x = picX - 1; x >= length; x--) {
			if (picIntArray[x][picY] != 0) {
				picIntArray[x + 1][picY] = picIntArray[x][picY];
				picIntArray[x][picY] = 0;
			}
		}

		for (int x = i - 1; x >= length; x--) {
			if (picBtnIntArray[x][j][0] != PIC_BTN_INT_ARRAY_EMPTY && picBtnIntArray[x][j][1] != PIC_BTN_INT_ARRAY_EMPTY) {
				int tempII = 0, tempJJ = 0;
				tempII = picBtnIntArray[x][j][0];
				tempJJ = picBtnIntArray[x][j][1];
				picBtnIntArray[x][j][0] = tempI;
				picBtnIntArray[x][j][1] = tempJ;
				tempI = tempII;
				tempJ = tempJJ;
			}

			Integer dy = map.get(x + "_" + j);
			if (dy == null) {
				dy = 0;
			}
			map.put(x + "_" + j, dy + 1);
			Action moveAction = Sequence.$(MoveTo.$(picBtnArray[x][j].x, picBtnArray[x][j].y - (dy + 1) * size, duration / 5));
			picBtnArray[x][j].action(moveAction);
		}
	}

	/**
	 * 向左移动
	 * 
	 * @param i
	 * @param j
	 * @param map
	 * @param length
	 */
	public void left(int i, int j, Map<String, Integer> map, int length) {
		// 向左移动一格
		int size = Gdx.graphics.getWidth() / X_NUMBER;
		int tempI = picBtnIntArray[i][j][0], tempJ = picBtnIntArray[i][j][1];

		// 更新picIntArray
		int picX = picBtnIntArray[i][j][0];
		int picY = picBtnIntArray[i][j][1];
		for (int y = picY + 1; y <= length; y++) {
			if (picIntArray[picX][y] != 0) {
				picIntArray[picX][y - 1] = picIntArray[picX][y];
				picIntArray[picX][y] = 0;
			}
		}

		picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
		picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;

		for (int y = j + 1; y <= length; y++) {
			if (picBtnIntArray[i][y][0] != PIC_BTN_INT_ARRAY_EMPTY && picBtnIntArray[i][y][1] != PIC_BTN_INT_ARRAY_EMPTY) {
				int tempII = 0, tempJJ = 0;
				tempII = picBtnIntArray[i][y][0];
				tempJJ = picBtnIntArray[i][y][1];
				picBtnIntArray[i][y][0] = tempI;
				picBtnIntArray[i][y][1] = tempJ;
				tempI = tempII;
				tempJ = tempJJ;
			}

			Integer dy = map.get(i + "_" + y);
			if (dy == null) {
				dy = 0;
			}
			map.put(i + "_" + y, dy + 1);
			Action moveAction = Sequence.$(MoveTo.$(picBtnArray[i][y].x - (dy + 1) * size, picBtnArray[i][y].y, duration / 5));
			picBtnArray[i][y].action(moveAction);
		}
	}

	/**
	 * 向右移动
	 * 
	 * @param i
	 * @param j
	 * @param map
	 * @param length
	 */
	public void right(int i, int j, Map<String, Integer> map, int length) {
		// 向右移动一格
		int size = Gdx.graphics.getWidth() / X_NUMBER;
		int tempI = picBtnIntArray[i][j][0], tempJ = picBtnIntArray[i][j][1];

		int picX = picBtnIntArray[i][j][0];
		int picY = picBtnIntArray[i][j][1];
		picBtnIntArray[i][j][0] = PIC_BTN_INT_ARRAY_EMPTY;
		picBtnIntArray[i][j][1] = PIC_BTN_INT_ARRAY_EMPTY;
		for (int y = picY - 1; y >= length; y--) {
			if (picIntArray[picX][y] != 0) {
				picIntArray[picX][y + 1] = picIntArray[picX][y];
				picIntArray[picX][y] = 0;
			}
		}

		for (int y = j - 1; y >= length; y--) {
			if (picBtnIntArray[i][y][0] != PIC_BTN_INT_ARRAY_EMPTY && picBtnIntArray[i][y][1] != PIC_BTN_INT_ARRAY_EMPTY) {
				int tempII = 0, tempJJ = 0;
				tempII = picBtnIntArray[i][y][0];
				tempJJ = picBtnIntArray[i][y][1];
				picBtnIntArray[i][y][0] = tempI;
				picBtnIntArray[i][y][1] = tempJ;
				tempI = tempII;
				tempJ = tempJJ;
			}

			Integer dy = map.get(i + "_" + y);
			if (dy == null) {
				dy = 0;
			}
			map.put(i + "_" + y, dy + 1);
			Action moveAction = Sequence.$(MoveTo.$(picBtnArray[i][y].x + (dy + 1) * size, picBtnArray[i][y].y, duration / 5));
			picBtnArray[i][y].action(moveAction);
		}
	}

	public boolean isGameStatus() {
		return gameStatus;
	}

	public void setGameStatus(boolean gameStatus) {
		this.gameStatus = gameStatus;
	}

	@SuppressLint("UseSparseArrays")
	public static Map<Integer, String> GAME_LEVEL_TEXT = new HashMap<Integer, String>();

	static {
		GAME_LEVEL_TEXT.put(1, "第一关");
		GAME_LEVEL_TEXT.put(2, "第二关");
		GAME_LEVEL_TEXT.put(3, "第三关");
		GAME_LEVEL_TEXT.put(4, "第四关");
		GAME_LEVEL_TEXT.put(5, "第五关");
		GAME_LEVEL_TEXT.put(6, "第六关");
		GAME_LEVEL_TEXT.put(7, "第七关");
		GAME_LEVEL_TEXT.put(8, "第八关");
		GAME_LEVEL_TEXT.put(9, "第九关");
		GAME_LEVEL_TEXT.put(10, "第十关");
		GAME_LEVEL_TEXT.put(11, "第十一关");
		GAME_LEVEL_TEXT.put(12, "第十二关");
	}

	@SuppressLint("UseSparseArrays")
	public static Map<Integer, String> GAME_LEVEL_DESC = new HashMap<Integer, String>();

	static {
		GAME_LEVEL_DESC.put(1, "新手试玩");
		GAME_LEVEL_DESC.put(2, "增加难度");
		GAME_LEVEL_DESC.put(3, "上下分离");
		GAME_LEVEL_DESC.put(4, "左右分离");
		GAME_LEVEL_DESC.put(5, "向左看齐");
		GAME_LEVEL_DESC.put(6, "地心引力");
		GAME_LEVEL_DESC.put(7, "飘向天空");
		GAME_LEVEL_DESC.put(8, "向右看齐");
		GAME_LEVEL_DESC.put(9, "左右集中");
		GAME_LEVEL_DESC.put(10, "上下集中");
		GAME_LEVEL_DESC.put(11, "四周分离");
		GAME_LEVEL_DESC.put(12, "中央集中");
	}

	@SuppressLint("UseSparseArrays")
	public static Map<Integer, Integer> GAME_LEVEL_PIC_NUMBERS = new HashMap<Integer, Integer>();

	static {
		GAME_LEVEL_PIC_NUMBERS.put(1, 14);
		GAME_LEVEL_PIC_NUMBERS.put(2, 18);
		GAME_LEVEL_PIC_NUMBERS.put(3, 18);
		GAME_LEVEL_PIC_NUMBERS.put(4, 18);
		GAME_LEVEL_PIC_NUMBERS.put(5, 18);
		GAME_LEVEL_PIC_NUMBERS.put(6, 18);
		GAME_LEVEL_PIC_NUMBERS.put(7, 18);
		GAME_LEVEL_PIC_NUMBERS.put(8, 18);
		GAME_LEVEL_PIC_NUMBERS.put(9, 18);
		GAME_LEVEL_PIC_NUMBERS.put(10, 18);
		GAME_LEVEL_PIC_NUMBERS.put(11, 18);
		GAME_LEVEL_PIC_NUMBERS.put(12, 18);
	}

	@SuppressLint("UseSparseArrays")
	public static Map<Integer, Integer> GAME_LEVEL_TOTAL_TIME = new HashMap<Integer, Integer>();

	static {
		GAME_LEVEL_TOTAL_TIME.put(1, 100);
		GAME_LEVEL_TOTAL_TIME.put(2, 100);
		GAME_LEVEL_TOTAL_TIME.put(3, 100);
		GAME_LEVEL_TOTAL_TIME.put(4, 100);
		GAME_LEVEL_TOTAL_TIME.put(5, 100);
		GAME_LEVEL_TOTAL_TIME.put(6, 100);
		GAME_LEVEL_TOTAL_TIME.put(7, 100);
		GAME_LEVEL_TOTAL_TIME.put(8, 100);
		GAME_LEVEL_TOTAL_TIME.put(9, 100);
		GAME_LEVEL_TOTAL_TIME.put(10, 100);
		GAME_LEVEL_TOTAL_TIME.put(11, 100);
		GAME_LEVEL_TOTAL_TIME.put(12, 100);
	}

	public int getSuccessNumber() {
		return successNumber;
	}

	public void setSuccessNumber(int successNumber) {
		this.successNumber = successNumber;
	}

	/**
	 * 清空消除记录
	 */
	public void clearSuccessNumber() {
		this.successNumber = 0;
	}

	/**
	 * 判断游戏是否死锁
	 */
	public boolean die(List<Point> path) {
		for (int i = 0; i < Y_NUMBER; i++) {
			for (int j = 0; j < X_NUMBER; j++) {
				if (picIntArray[i][j] != 0) // 如果当前位置没有图片
				{
					for (int l = j + 1; l < X_NUMBER; l++) // 和同一行剩余的点比较
					{
						if (picIntArray[i][l] == picIntArray[i][j] && link(new int[] { i, j }, new int[] { i, l }, path)) {
							Log.i("zhy", "picIntArray[i][l] = " + picIntArray[i][l]);
							Log.i("zhy", "picIntArray[i][j] = " + picIntArray[i][j]);
							Log.i("zhy", "i = " + i + " l = " + l + " j = " + j);
							return false;
						}
					}
					for (int k = i + 1; k < Y_NUMBER; k++) // 和剩余的行比较
					{
						for (int l = 0; l < X_NUMBER; l++) {
							if (picIntArray[k][l] == picIntArray[i][j] && link(new int[] { i, j }, new int[] { k, l }, path)) {
								Log.i("zhy", "picIntArray[k][l] = " + picIntArray[k][l]);
								Log.i("zhy", "picIntArray[i][j] = " + picIntArray[i][j]);
								Log.i("zhy", "i = " + i + " j = " + j + "l = " + l + " k = " + k);
								return false;
							}
						}
					}
				}
			}
		}
		Log.i("zhy", "die()");
		return true;
	}

	/**
	 * 游戏死锁，刷新棋盘
	 */
	@SuppressLint("UseSparseArrays")
	public boolean change() {
		if (refresh == 0) {
			return false;
		}
		// 清空选择图片
		firstPosition = null;
		selectorBtn.x = Gdx.graphics.getWidth();
		selectorBtn.y = Gdx.graphics.getHeight();
		selectorBtn2.x = Gdx.graphics.getWidth(); // 取消第二个图片的选择
		selectorBtn2.y = Gdx.graphics.getHeight();

		refresh--;
		int[] arr = new int[XY_NUMBER];
		int increaseZero = 0, increaseOther = 0;
		// 统计现有的图片种类
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int x = 0; x < Y_NUMBER; x++) {
			for (int y = 0; y < X_NUMBER; y++) {
				if (picIntArray[x][y] == 0) {
					increaseZero++;
					continue;
				}
				arr[increaseOther] = x * X_NUMBER + y;
				increaseOther++;

				if (map.get(picIntArray[x][y]) == null) {
					map.put(picIntArray[x][y], 1);
				} else {
					map.put(picIntArray[x][y], map.get(picIntArray[x][y]) + 1);
				}
			}
		}

		increaseZero = increaseZero / 2;

		// 重新布局
		for (Integer number : map.keySet()) {
			for (int i = 0; i < map.get(number) / 2; i++, increaseZero++) {
				int position1 = MathUtils.random(0, XY_NUMBER - 1 - increaseZero * 2);
				picIntArray[arr[position1] / X_NUMBER][arr[position1] % X_NUMBER] = number;
				arr[position1] = arr[XY_NUMBER - 1 - increaseZero * 2];
				int position2 = MathUtils.random(0, XY_NUMBER - 2 - increaseZero * 2);
				picIntArray[arr[position2] / X_NUMBER][arr[position2] % X_NUMBER] = number;
				arr[position2] = arr[XY_NUMBER - 2 - increaseZero * 2];
			}
		}

		if (die(new ArrayList<Point>())) {// 如果是死局的话重新布局
			return change();
		} else {
			for (int i = 0; i < Y_NUMBER; i++) {
				for (int j = 0; j < X_NUMBER; j++) {
					if (picBtnArray[i][j] != null) {
						stage.removeActor(picBtnArray[i][j]);
					}
				}
			}
			// 生成图片
			initGameBtn(false);

			for (int i = 0; i < picIntArray.length; i++) {
				String str = "";
				for (int j = 0; j < picIntArray[i].length; j++) {
					str += picIntArray[i][j] + "\t";
				}
				Log.e("change", str);
			}

			for (int i = 0; i < picBtnIntArray.length; i++) {
				String str = "";
				for (int j = 0; j < picBtnIntArray[i].length; j++) {
					str += picBtnIntArray[i][j][0] + "_" + picBtnIntArray[i][j][1] + "\t";
				}
				Log.e("change", str);
			}
		}
		return true;
	}

	/**
	 * 提示2个相同的图片
	 */
	public boolean tip() {
		if (help == 0) {
			return false;
		} else {
			// soundPlay.play(ID_SOUND_TIP, 0);
			help--;
			List<Point> path = new ArrayList<Point>();
			if (!die(path)) // 判断一下是否死锁，为了填充path变量值
			{
				Point first = null;
				Point second = null;
				if (path != null && path.size() >= 2) {
					first = path.get(0);
					second = path.get(path.size() - 1);
					Log.i("zhy", "path.size() =  " + path.size());
					Log.i("zhy", "first.x = " + first.x + "  first.y = " + first.y + "  second.x = " + second.x + "  second.y = " + second.y);
					selectorBtn.x = (first.x - 1) * iconSize - 10; // 画图坐标系和Point坐标关于水平线
					                                               // 对称
					selectorBtn.y = (Y_NUMBER - first.y) * iconSize - 10;
					selectorBtn2.x = (second.x - 1) * iconSize - 10;
					selectorBtn2.y = (Y_NUMBER - second.y) * iconSize - 10;
				}
				return true;
			} else {
				return false;
			}
		}
	}

	public int getRefreshNum() {
		return refresh;
	}

	public int getHelpNum() {
		return help;
	}
}
